package com.fhnew.app.ui.activity.userinfo.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.fhnew.app.R;
import com.fhnew.app.global.Constants;
import com.fhnew.app.model.CustomObserver;
import com.fhnew.app.model.internet.model.UserModel;
import com.fhnew.app.model.network.HttpJsonUtils;
import com.fhnew.app.model.network.entity.req.BankList;
import com.fhnew.app.ui.dialogactivity.BankActivity;
import com.fhnew.app.ui.dialogactivity.UnbankDialog;
import com.fhnew.app.ui.dialogactivity.WithdrawDialog;
import com.fhnew.app.util.OnItemClickListener;
import com.fhnew.app.util.SYSOUtils;

import java.util.ArrayList;

import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Administrator on 2019/9/12.
 */

public class CardFragment extends Fragment {

    ImageView bankimg;
    RelativeLayout addbank;
    RecyclerView banklist;
    private Adapter adapter;
    private GridLayoutManager layoutManager;
    private ArrayList<BankList.DataBean.ListBean> dataBean = new ArrayList<>();

    private void initView (View view) {
        bankimg = view.findViewById(R.id.bankimg);
        addbank = view.findViewById(R.id.addbank);
        banklist = view.findViewById(R.id.banklist);
    }

    @Nullable
    @Override
    public View onCreateView (@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        container = (ViewGroup) inflater.inflate(R.layout.cardlayout, null);
        initView(container);
        return container;
    }

    @Override
    public void onViewCreated (@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getUserBankList();
        layoutManager = new GridLayoutManager(getContext(), 2, LinearLayoutManager.VERTICAL, false);
        banklist.setLayoutManager(layoutManager);
        adapter = new Adapter();
        banklist.setAdapter(adapter);
    }

    @Override
    public void onResume () {
        super.onResume();
        getUserBankList();
    }

    private void getUserBankList () {
        HttpJsonUtils.getHttpApiInstance()
                .getUserBankList(Constants.getuseAdress() + "api/m/user/getUserBankList", UserModel.Token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<BankList>() {
                    @Override
                    protected void onCustomNext (BankList bankList) {
                        if (bankList.code == 0 && bankList.data.list.size() > 0) {
                            addbank.setVisibility(View.GONE);
                            banklist.setVisibility(View.VISIBLE);
                            dataBean.clear();
                            dataBean.addAll(bankList.data.list);
                            adapter.notifyDataSetChanged();
                        } else {
                            addbank.setVisibility(View.VISIBLE);
                            banklist.setVisibility(View.GONE);
                        }
                    }

                    @Override
                    protected void onSubscribeCustom (Disposable disposable) {

                    }

                    @Override
                    protected void onErrorCustom (Throwable e) {
                        SYSOUtils.sOut(e.toString());
                    }
                });

    }

    @OnClick(R.id.addbank)
    public void onViewClicked () {
        Intent intentm = new Intent(getContext(), BankActivity.class);
        startActivity(intentm);
    }

    class Adapter extends RecyclerView.Adapter<Adapter.MViewHolder>
            implements OnItemClickListener {
        @NonNull
        @Override
        public Adapter.MViewHolder onCreateViewHolder (@NonNull ViewGroup viewGroup, int i) {
            return new MViewHolder(LayoutInflater.from(getContext())
                    .inflate(R.layout.banitem, viewGroup, false));
        }

        @Override
        public void onBindViewHolder (@NonNull Adapter.MViewHolder holder, int position) {

            if (position < dataBean.size()) {
                holder.itemaddbank1.setVisibility(View.VISIBLE);
                holder.itemaddbank.setVisibility(View.GONE);
                holder.itembankname.setText(dataBean.get(position).bank_name);
                holder.itembancode.setText(dataBean.get(position).cardNo);
                holder.itembankaddress.setText(dataBean.get(position).subAddress);
                holder.itembt.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick (View v) {
                        //取款
                        Intent intent = new Intent(getContext(), WithdrawDialog.class);
                        intent.putExtra("bid", dataBean.get(position).bid + "");
                        startActivity(intent);
                    }
                });
                holder.itemunbind.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick (View v) {
                        if (!TextUtils.isEmpty(UserModel.user.isPhone)) {
                            Intent intent = new Intent(getContext(), UnbankDialog.class);
                            intent.putExtra("bid", dataBean.get(position).bid + "");
                            startActivity(intent);
                        } else {
                            Toast.makeText(getContext(), "请先在个人资料绑定手机号", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            } else {
                if (dataBean.size() < 3) {
                    holder.itemaddbank1.setVisibility(View.GONE);
                    holder.itemaddbank.setVisibility(View.VISIBLE);
                } else {
                    holder.itemaddbank1.setVisibility(View.GONE);
                    holder.itemaddbank.setVisibility(View.GONE);
                }

            }
            holder.itemaddbank.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick (View v) {
                    if (!TextUtils.isEmpty(UserModel.user.isPhone)) {
                        Intent intentm = new Intent(getContext(), BankActivity.class);
                        startActivity(intentm);
                    } else {
                        Toast.makeText(getContext(), "请先在个人资料绑定手机号", Toast.LENGTH_SHORT).show();
                    }
                }
            });

        }

        @Override
        public int getItemCount () {
            return dataBean.size() + 1;
        }

        @Override
        public void onItemClick (View view, int position) {

        }

        public class MViewHolder extends RecyclerView.ViewHolder {
            RelativeLayout itemaddbank;
            LinearLayout itemaddbank1;
            TextView itembankname, itemunbind, itembancode, itembankaddress, itembt;

            public MViewHolder (@NonNull View itemView) {
                super(itemView);
                itemaddbank = (RelativeLayout) itemView.findViewById(R.id.itemaddbank);
                itemaddbank1 = itemView.findViewById(R.id.itemaddbank1);
                itembankname = itemView.findViewById(R.id.itembankname);
                itemunbind = itemView.findViewById(R.id.itemunbind);//解绑
                itembancode = itemView.findViewById(R.id.itembancode);
                itembankaddress = itemView.findViewById(R.id.itembankaddress);
                itembt = itemView.findViewById(R.id.itembt);//设为默认
            }
        }
    }


}
